//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.live.app.service;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.live.app.jwt.JWTUtil;
import com.ruoyi.live.base.domain.LiveBarrage;
import com.ruoyi.live.base.domain.LiveProductSource;
import com.ruoyi.live.base.domain.User;
import com.ruoyi.live.base.mapper.LiveBarrageMapper;
import com.ruoyi.live.base.mapper.LiveProductSourceMapper;
import com.ruoyi.live.base.mapper.UserMapper;
import com.ruoyi.live.base.service.IUserService;
import com.ruoyi.live.tool.commons.ICommons;
import com.ruoyi.live.tool.commons.IMD5;
import com.ruoyi.live.tool.utils.Message;
import com.ruoyi.live.tool.utils.PicUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import jakarta.servlet.http.HttpServletRequest;
import java.util.*;

@Service
public class OpenService {
    @Autowired
    UserMapper userDao;
    @Autowired
    LiveProductSourceMapper sourceDao;
    @Autowired
    LiveBarrageMapper barrageMapper;
    @Autowired
    CacheService cacheService;
    public static String urlPath = "";
    public static String uploadUrlPath = "";
    @Autowired
    IUserService interUserService;
    @Autowired
    RestTemplate restTemplate;

    @Autowired
    JWTUtil jwtUtil;

    public OpenService() {
    }

    public Message appLogin(String username, String password) {
        if (!ICommons.isNullOrEmpty(username) && !ICommons.isNullOrEmpty(password)) {
            Long ULoginNameMd5 = IMD5.getMD5(username.trim());
            Long ULoginPassword = IMD5.getMD5(password.trim());
            User user = (User)this.userDao.selectOne(new QueryWrapper((new User()).setUserLoginName(username.trim()).setUserPasswordMd5(ULoginPassword)));
            String token;
            if (user == null && "1".equals(this.cacheService.getParam("SYSTEM_APP_LOGIN_ASYNC"))) {
                try {
                    token = this.cacheService.getParam("ZTC_URL") + "?loginName=" + username.trim() + "&commodityId=" + this.cacheService.getParam("ZTC_ID");
                    String res = (String)this.restTemplate.getForObject(token, String.class, new Object[0]);
                    JSONObject obj = JSON.parseObject(res);
                    if (!obj.getBoolean("succ")) {
                        return new Message(false, "请到职通车申请课程");
                    }

                    user = new User();
                    user.setUserLoginName(username.trim()).setUserLoginNameMd5(ULoginNameMd5).setUserRealName(username.trim()).setUserPasswordMd5(ULoginPassword).setUserCreateUserId(2L).setUserType(3).setUserState(1).setClassId(1L);
                    this.userDao.insert(user);
                } catch (Exception var10) {
                    var10.printStackTrace();
                    return new Message(false, "用户名/密码错误");
                }
            }

            if (user == null) {
                return new Message(false, "用户名/密码错误");
            } else {
                try {
                    if (user.getUserState() != 1) {
                        return new Message(false, "该用户被锁定");
                    } else {
                        token = jwtUtil.sign(user.getUserLoginName(), Long.toString(user.getUserPasswordMd5()));
                        user.setToken(token);
                        user.setUrlPath(urlPath);
                        return new Message(true, JSON.toJSONString(user));
                    }
                } catch (Exception var9) {
                    var9.printStackTrace();
                    return new Message(false, "登录失败");
                }
            }
        } else {
            return new Message(false, "用户名、密码不能为空");
        }
    }

    public void initImage(HttpServletRequest request) {
        List<LiveProductSource> list = this.sourceDao.selectList(new QueryWrapper(new LiveProductSource()));
        Iterator var3 = list.iterator();

        while(var3.hasNext()) {
            LiveProductSource source = (LiveProductSource)var3.next();
            String url = request.getSession().getServletContext().getRealPath("/static/static/data/") + source.getProductId() + "/" + source.getSourceType() + "/" + source.getSourceStoreName();
            Map<String, Integer> map = PicUtils.getImgSize(url);
            source.setWidth((Integer)map.get("width"));
            source.setHeight((Integer)map.get("height"));
            this.sourceDao.updateById(source);
        }

    }

    public void insertBody(Integer count) {
        Map<Integer, String> res = new HashMap();
        res.put(5, "游艇");
        res.put(1, "天使");
        res.put(7, "摩天轮");
        res.put(6, "礼炮");
        res.put(4, "城堡");
        res.put(2, "嘴唇");
        res.put(3, "印钞机");
        Random random = new Random();
        Integer[] ints = new Integer[]{1, 1, 1, 1, 1, 5, 10, 20, 50, 99};

        for(int j = 0; j <= 49; ++j) {
            int giftIndex = random.nextInt(7) + 1;
            int giftCount = ints[random.nextInt(10)];
            int headerImg = random.nextInt(36) + 1;
            String content = giftIndex + "," + (String)res.get(giftIndex) + "," + giftCount + "," + headerImg;
            this.barrageMapper.insert((new LiveBarrage()).setCreateTime(System.currentTimeMillis()).setCreateUser(1L).setState(1).setType(3).setContent(content));
        }

    }
}
